package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.AdsDealModel;
import models.DealModel;
import models.OrderModel;
import search.OrderSearchData;
import admin.data.AdsDealData;
import admin.data.DealData;
import admin.data.OrderData;

@SuppressWarnings("serial")
public class ProcessDealServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		try {
			TimeZone localTimeZone = TimeZone.getTimeZone("GMT+07:00");
			TimeZone.setDefault(localTimeZone);
			Calendar.getInstance().setTimeZone(localTimeZone);
			String action = (String) request.getParameter("action");
			String strIds = (String) request.getParameter("ids");
			String[] ids = null;
			if (strIds != null) {
				ids = strIds.split(",");
			}
			if (action != null && !action.isEmpty()) {
				if (action.equals("delete")) {
					// for (int i = 0; i < ids.length; i++) {
					// if (!ids[i].isEmpty()) {
					// DealModel deal = DealData.getById(ids[i]);
					// DealData.delete(deal);
					// }
					// }
				} else if (action.equals("lock")) {
					for (int i = 0; i < ids.length; i++) {
						if (!ids[i].isEmpty()) {
							DealModel deal = DealData.getById(ids[i]);
							deal.setStatus(4);
							DealData.insert(deal);
							OrderSearchData orderSearch = new OrderSearchData();
							int page = 1;
							orderSearch.setLimit(488);
							while (true) {
								orderSearch.setPage(page);
								orderSearch.searchByDealId(deal.getDealId());
								for (OrderModel order : orderSearch
										.getListResult()) {
									if (order.getStatus() != 3
											&& order.getStatus() != 4) {
										order.setOldStatus(order.getStatus());
										order.setStatus(8);
										System.out
												.println(order.getOrderDate());
										OrderData.insert(order);
									}
								}
								if (orderSearch.getListResult().size() < 488) {
									break;
								} else {
									page++;
								}
							}
						}
					}
				} else if (action.equals("unlock")) {
					for (int i = 0; i < ids.length; i++) {
						if (!ids[i].isEmpty()) {
							DealModel deal = DealData.getById(ids[i]);
							deal.setStatus(5);
							DealData.insert(deal);
							OrderSearchData orderSearch = new OrderSearchData();
							int page = 1;
							orderSearch.setLimit(488);
							while (true) {
								orderSearch.setPage(page);
								orderSearch.searchByDealId(deal.getDealId());
								for (OrderModel order : orderSearch
										.getListResult()) {
									if (order.getStatus() == 8) {
										order.setStatus(order.getOldStatus());
										OrderData.insert(order);
									}
								}
								if (orderSearch.getListResult().size() < 488) {
									break;
								} else {
									page++;
								}
							}
						}
					}
				} else if (action.equals("specialAdvertising")) {
					if (ids != null && ids.length > 0) {
						AdsDealModel adTop = AdsDealData.getByType("special");
						List<String> listDealId = new ArrayList<String>();
						if (adTop == null) {
							adTop = new AdsDealModel();
							adTop.setType("special");
						}
						DealModel deal = DealData.getActiveById(ids[0]);
						if (deal != null) {
							listDealId.add(ids[0]);
						}
						adTop.setListDealId(listDealId);
						AdsDealData.insert(adTop);
					}
				} else if (action.equals("topAdversiting")) {
					if (ids != null && ids.length > 0) {
						AdsDealModel adTop = AdsDealData.getByType("top");
						List<String> listDealId = new ArrayList<String>();
						if (adTop == null) {
							adTop = new AdsDealModel();
							adTop.setType("top");
						}
						listDealId = adTop.getListDealId();
						for (String strId : ids) {
							DealModel deal = DealData.getActiveById(strId);
							if (deal != null) {
								if (!listDealId.contains(deal.getDealId())) {
									if (listDealId.size() == 6) {
										listDealId.remove(0);
									}
									listDealId.add(strId);
								}
							}
						}
						adTop.setListDealId(listDealId);
						AdsDealData.insert(adTop);
					}
				} else if (action.equals("rightAdversiting")) {
					if (ids != null && ids.length > 0) {
						AdsDealModel adTop = AdsDealData.getByType("right");
						List<String> listDealId = new ArrayList<String>();
						if (adTop == null) {
							adTop = new AdsDealModel();
							adTop.setType("right");
						}
						listDealId = adTop.getListDealId();
						for (String strId : ids) {
							DealModel deal = DealData.getActiveById(strId);
							if (deal != null) {
								if (!listDealId.contains(deal.getDealId())) {
									if (listDealId.size() == 20) {
										listDealId.remove(0);
									}
									listDealId.add(strId);
								}
							}
						}
						adTop.setListDealId(listDealId);
						AdsDealData.insert(adTop);
					}
				} else if (action.equals("orderRightMenu")) {
					String currentId = request.getParameter("currentId");
					String prevId = request.getParameter("prevId");
					String nextId = request.getParameter("nextId");
					AdsDealModel adTop = AdsDealData.getByType("right");
					List<String> listDealId = new ArrayList<String>();
					if (prevId == null) {
						listDealId.add(currentId);
						for (String string : adTop.getListDealId()) {
							if (string.equals(currentId)) {
								continue;
							} else {
								listDealId.add(string);
							}
						}
					} else if (nextId == null) {
						for (String string : adTop.getListDealId()) {
							if (string.equals(currentId)) {
								continue;
							} else {
								listDealId.add(string);
							}
						}
						listDealId.add(currentId);
					} else {
						for (String string : adTop.getListDealId()) {
							if (string.equals(currentId)) {
								continue;
							}
							listDealId.add(string);
							if (string.equals(prevId)) {
								listDealId.add(currentId);
							}
						}
					}
					adTop.setListDealId(listDealId);
					AdsDealData.insert(adTop);
				} else if (action.equals("removeRightAdversiting")) {
					if (ids != null && ids.length > 0) {
						AdsDealModel adTop = AdsDealData.getByType("right");
						List<String> listDealId = new ArrayList<String>();
						if (adTop != null) {
							listDealId = adTop.getListDealId();
							if (listDealId.contains(ids[0])) {
								listDealId.remove(ids[0]);
							}
							adTop.setListDealId(listDealId);
							AdsDealData.insert(adTop);
							HttpSession session = request.getSession();
							session.setAttribute("success",
									"Thao tác thành công !");
						}
					}
				} else if (action.equals("removeTopAdversiting")) {
					if (ids != null && ids.length > 0) {
						AdsDealModel adTop = AdsDealData.getByType("top");
						List<String> listDealId = new ArrayList<String>();
						if (adTop != null) {
							listDealId = adTop.getListDealId();
							if (listDealId.contains(ids[0])) {
								listDealId.remove(ids[0]);
							}
							adTop.setListDealId(listDealId);
							AdsDealData.insert(adTop);
							HttpSession session = request.getSession();
							session.setAttribute("success",
									"Thao tác thành công !");
						}
					}
				}

			}
		} catch (Exception e) {

		}

	}
}
